/*
 * UpdateMPDDialog.java
 *
 * Created on March 28, 2006, 7:24 PM
 */
package com.bff.slimserver.musicjukebox.ui.database;

import com.bff.slimserver.musicjukebox.lib.JukeboxProperties;
import com.bff.slimserver.musicjukebox.ui.components.MusicJDialog;
import com.bff.bjj.ui.component.BJJButton;
import com.bff.bjj.utils.Utils;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import org.bff.slimserver.SlimDatabase;

/**
 *
 * @author  Bill
 */
public class SlimUpdateDialog extends MusicJDialog {

    /** Creates new form UpdateMPDDialog */
    public SlimUpdateDialog(java.awt.Frame parent, boolean clearAndScan) {
        super(parent);
        setUndecorated(true);
        initComponents();
        Utils.centerInParent(this, parent);
        try {
            if (JukeboxProperties.getInstance().getDatabase().isRescanning()) {
                JOptionPane.showMessageDialog(this, "SqueezeCenter is currently updating.");
            } else {

                if (clearAndScan) {
                    JukeboxProperties.getInstance().getDatabase().clearDatabaseAndRescan();
                } else {
                    JukeboxProperties.getInstance().getDatabase().rescan();
                }

                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        while(!isVisible()) {
                            try {
                                Thread.sleep(200);
                            } catch (InterruptedException ex) {
                                Logger.getLogger(SlimUpdateDialog.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                        try {
                            Thread.sleep(3000);
                        } catch (InterruptedException ex) {
                            Logger.getLogger(SlimUpdateDialog.class.getName()).log(Level.SEVERE, null, ex);
                        }

                        dispose();
                    }
                }).start();
                setVisible(true);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("SqueezeCenter Update");
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
        });
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 102, 255)));
        jPanel1.setPreferredSize(new java.awt.Dimension(300, 100));
        jPanel1.setLayout(new java.awt.BorderLayout());

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("SqueezeCenter update has been started.");
        jPanel1.add(jLabel1, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel1, new java.awt.GridBagConstraints());

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
        dispose();
    }//GEN-LAST:event_formMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
}
